import java.util.Vector;

import javax.swing.JOptionPane;


public class GameObject 
{
	public static Vector<GameObject> gameObjects = new Vector<GameObject>();
	static MapSquare map[][];
	int x=0;
	int y=0;
	int oldX =0; 
	// we'll store the position since last update, so we can make sure to update that cell too
	int oldY =0;
	String filePath;	
	String description;
	static Player player;
	static int regenCounter = 0;
	//boolean observable = true;
	
	static void loadNewMap(MapSquare[][] map)
	{
		if(map == null) // just a couple simple checks
		{
			return;
		}
		if(map.length != 13|| map[0].length != 13)
		{
			return;
		}
		GameObject.map = map;
		gameObjects = new Vector<GameObject>();
		if(GameObject.player != null)
		{
			gameObjects.add(GameObject.player);
		}
		for(int i = 0; i< 13; i++)
		{
			for(int j = 0; j< 13; j++)
			{
				GameObject go = map[i][j].gameObject;
				if(go != null)
				{
					gameObjects.add(go);
				}
			}
		}
		
	}
	
	static void updateAll()
	{
		int p = gameObjects.size();
		for(int i = 0; i < p; i++)
		{
			GameObject go = gameObjects.get(i);
			if(go instanceof Player)
			{
				Player player = (Player)go;
				if(player.currentHP<=0)
				{
					
					if(player == GameObject.player)
					{
						gameOver();
						player.filePath = "./images/Toot/tootDead.png";
						Loot l = new Loot(player.x, player.y, false, 
								"deadToot", "./images/Toot/tootDead.png", Element.Earth, LootEnum.Other );
						GameObject.gameObjects.add(l);	
						GameObject.map[player.x][player.y].update();	
					}
					else
					{
						Mob m = (Mob) player;
						if (m.loot != null)
						{
							Loot l = LootCreator.Copy(m.x,m.y,m.loot);
							GameObject.map[m.x][m.y].gameObject = l;
							GameObject.gameObjects.add(l);	
							GameObject.map[m.x][m.y].update();
						}
					}
					player.x = 20;
					player.y = 20;
				}
			}
			if(go.x >= 14 || go.y >= 14)
			{
				gameObjects.removeElementAt(i);
				p--;
				i--;
			}
		}
		for(int i = 0; i <gameObjects.size(); i++)
		{
			GameObject go = gameObjects.get(i);
			if(go instanceof Updateable)
			{
				((Updateable)go).update();
			}
		}
		if (regenCounter == 30)
		{
			regenCounter = 0;
			if (GameObject.player.activeSpell.element == Element.Fire)
			{
				if (GameObject.player.firePower < 25)
				{
					GameObject.player.firePower += 5;
					mainFrame.addElement(2);
				}
			}
			if (GameObject.player.activeSpell.element == Element.Water)
			{
				if (GameObject.player.waterPower < 25)
				{
					GameObject.player.waterPower += 5;
					mainFrame.addElement(1);
				}
			}
			if (GameObject.player.activeSpell.element == Element.Earth)
			{
				if (GameObject.player.earthPower < 25)
				{
					GameObject.player.earthPower += 5;
					mainFrame.addElement(3);
				}
				
			}
			if (GameObject.player.activeSpell.element == Element.Lightning)
			{
				if (GameObject.player.lightningPower < 25)
				{
					GameObject.player.lightningPower += 5;
					mainFrame.addElement(4);
				}
				
			}
		}
		else
			regenCounter++;
	}
	
	private static void gameOver() 
	{
		GameObject.player.filePath = "src/images/toot/tootDead.png";	
		JOptionPane error=new JOptionPane();
		JOptionPane.showMessageDialog(error,"You die", "Lose", JOptionPane.ERROR_MESSAGE);
		System.exit(-1);
	}

	static void printGame()
	{
		char game[][] = new char[13][13];
		
		for(int i = 0; i < 13; i++)
			for(int j = 0; j < 13; j++)
			{
				game[i][j] = '-';
			}
		
		//for(GameObject go : gameObjects)
		for(int i = 0; i < gameObjects.size(); i++)
		{
			GameObject go = gameObjects.get(i);
			char a = '-';
			if(go instanceof Mob)
			{
				a = 'M';
			}
			else if(go instanceof Player)
			{
				a = 'P';
			}
			else if(go instanceof Boundary)
			{
				a = 'B';
			}
			else if(go instanceof Spell)
			{
				a = 'S';
			}
			else if(go instanceof Exit)
			{
				a = 'X';
			}
			if (go.x >=0 &&go.x <=12 && go.y >=0 && go.y <=12)
			{
				game[go.x][go.y] = a;
			}
		}
		System.out.print('\n');		
		for(int i = 0; i < 13; i++)
		{
			for(int j = 0; j < 13; j++)
			{
				System.out.print(game[j][i]);
			}
			System.out.print('\n');
		}
	}
}
